﻿using JSTest;
using JSTest.ScriptElements;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Xrm.Framework.Test.Unit
{
    public abstract class XrmJsUnitTest : XrmUnitTest
    {
        #region Internal attributes

        private TestScript _testScript;

        #endregion

        #region Protected properties

        protected TestCase TestCase { get; set; }

        protected TestCase[] TestCases { get; set; }

        #endregion

        #region Constructors

        protected XrmJsUnitTest()
            :this(false)
        {
        }

        protected XrmJsUnitTest(Boolean includeDefaultBreakpoint)
            : base()
        {
            this._testScript = new TestScript() { IncludeDefaultBreakpoint = includeDefaultBreakpoint }; 
        }

        protected XrmJsUnitTest(TimeSpan timeSpan)
            : this(timeSpan,false)
        {
        }

        protected XrmJsUnitTest(TimeSpan timeSpan, Boolean includeDefaultBreakpoint)
            : base()
        {
            this._testScript = new TestScript(timeSpan) { IncludeDefaultBreakpoint = includeDefaultBreakpoint };
        }

        #endregion

        #region Override base methods

        protected override void Do()
        {
            if (TestCase != null)
            {
                this.AppendFile(TestCase.TestFile);
                this._testScript.RunTest(TestCase);
            }
            else if(TestCases!=null && TestCases.Length > 0)
            {
                foreach (TestCase tc in TestCases)
                {
                    this.AppendFile(tc.TestFile);
                    this._testScript.RunTest(tc);
                }
            }
        }

        protected override void Verify()
        {
            //Do nothing
        }

        #endregion

        #region New helper methods

        protected virtual void AppendBlock(ScriptBlock scriptBlock)
        {
            this._testScript.AppendBlock(scriptBlock);
        }

        protected virtual void AppendFile(string filePath)
        {
            this._testScript.AppendFile(filePath);
        }

        protected virtual void AppendFile(ScriptInclude scriptInclude)
        {
            this._testScript.AppendFile(scriptInclude);
        }

        #endregion
    }
}
